<?php
    $owner = $collection->owner();

    if ($collection->type === 'feed') {
        $open_graph = [
            'title' => $collection->name,
            'description' => _f('A feed from %s, on %s.', protect($collection->feedWebsite()), get_app_configuration('brand')),
            'locale' => $owner->locale,
            'url' => url_full('collection', ['id' => $collection->id]),
        ];

        $feed = [
            'type' => 'application/atom+xml',
            'url' => $collection->feed_url,
            'title' => _f('Syndication feed of %s', $collection->name),
        ];
    } else {
        $open_graph = [
            'title' => $collection->name,
            'description' => _f('A collection created by %s on %s.', $owner->username, get_app_configuration('brand')),
            'locale' => $owner->locale,
            'url' => url_full('collection', ['id' => $collection->id]),
        ];

        $feed = [
            'type' => 'application/atom+xml',
            'url' => url_full('collection feed', ['id' => $collection->id]),
            'title' => _f('Syndication feed of %s', $collection->name),
        ];
    }

    $current_tab = 'feeds';

    if ($collection->image_filename) {
        $open_graph['image'] = url_media_full('covers', $collection->image_filename);
    }

    if ($pagination->isCurrentFirstPage()) {
        $current_url_params = ['id' => $collection->id];
    } else {
        $current_url_params = [
            'id' => $collection->id,
            'page' => $pagination->currentPage(),
        ];
    }

    $this->layout('base.phtml', [
        'title' => $collection->name,
        'canonical' => url_full('collection', $current_url_params),
        'current_tab' => $current_tab,
        'alternates' => [$feed],
        'open_graph' => $open_graph,
    ]);
?>

<div class="section">
    <?php if ($collection->image_filename): ?>
        <img
            class="section__image"
            alt=""
            src="<?= url_media('large', $collection->image_filename) ?>"
        />
    <?php endif; ?>

    <div class="section__title">
        <h1><?= protect($collection->name) ?></h1>
    </div>

    <?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

    <?php if ($topics): ?>
        <p class="collection__topics">
            <?= implode('&nbsp;·&nbsp;', array_column($topics, 'label')) ?>
        </p>
    <?php endif; ?>

    <div class="section__meta">
        <?php if ($collection->type === 'feed'): ?>
            <div class="section__details">
                <p class="collection__feed">
                    <?= icon('feed') ?>
                    <?php if ($collection->feed_site_url): ?>
                        <?= _f('Feed from <a target="_blank" rel="noopener noreferrer" href="%s">%s</a>', protect($collection->feed_site_url), protect($collection->feedWebsite())) ?>
                    <?php else: ?>
                        <?= _f('Feed from <a target="_blank" rel="noopener noreferrer" href="%s">%s</a>', protect($collection->feed_url), protect($collection->feedWebsite())) ?>
                    <?php endif; ?>
                </p>

                <p title="<?= _('Publication frequency') ?>">
                    <?= icon('line-chart') ?>
                    <?= format_publication_frequency($collection->publication_frequency_per_year) ?>
                </p>
            </div>
        <?php else: ?>
            <div class="section__details">
                <p class="collection__owner">
                    <?php if (!$collection->is_public): ?>
                        <span class="collection__public sticker"><?= _('private') ?></span>
                    <?php endif; ?>

                    <?= _f('Published by %s', collection_publishers($collection, $current_user)) ?></li>
                </p>

                <p title="<?= _('Publication frequency') ?>">
                    <?= icon('line-chart') ?>
                    <?= format_publication_frequency($collection->publication_frequency_per_year) ?>
                </p>
            </div>
        <?php endif; ?>

        <div class="section__actions">
            <?php if ($current_user): ?>
                <?php if ($current_user->isFollowing($collection->id)): ?>
                    <details
                        class="popup"
                        data-controller="popup"
                        data-action="toggle->popup#update click@window->popup#closeOnClickOutside keydown->popup#closeOnEscape"
                    >
                        <summary class="popup__opener">
                            <span class="button button--small">
                                <?= icon('menu') ?>
                                <?= _('Actions') ?>
                            </span>
                        </summary>

                        <nav class="popup__container popup__container--left" role="menu">
                            <button
                                class="popup__item popup__item--button"
                                data-controller="modal-opener"
                                data-action="modal-opener#fetch"
                                data-modal-opener-href-value="<?= url('edit collection filter', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                                aria-haspopup="dialog"
                                aria-controls="modal"
                                role="menuitem"
                            >
                                <?= icon('slider') ?>
                                <?= _('Adjust for the news') ?>
                            </button>

                            <button
                                class="popup__item popup__item--button"
                                data-controller="modal-opener"
                                data-action="modal-opener#fetch"
                                data-modal-opener-href-value="<?= url('edit group collection', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                                aria-haspopup="dialog"
                                aria-controls="modal"
                                role="menuitem"
                            >
                                <?= icon('directory') ?>
                                <?= _('Put in a group') ?>
                            </button>

                            <div data-controller="copy-to-clipboard">
                                <input type="hidden" value="<?= url_full('collection', ['id' => $collection->id]) ?>" data-copy-to-clipboard-target="copyable" />

                                <button
                                    class="popup__item popup__item--button share__button"
                                    data-action="copy-to-clipboard#copy"
                                    data-copy-to-clipboard-target="feedback"
                                    role="menuitem"
                                >
                                    <?= icon('copy-to-clipboard') ?>
                                    <?= _('Copy the link') ?>
                                </button>
                            </div>

                            <div class="popup__separator"></div>

                            <form method="post" action="<?= url('mark collection as read', ['id' => $collection->id]) ?>" role="menuitem">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />

                                <button class="popup__item popup__item--button">
                                    <?= icon('check') ?>
                                    <?= _('Mark all as read') ?>
                                </button>
                            </form>

                            <form method="post" action="<?= url('never read collection', ['id' => $collection->id]) ?>" role="menuitem">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />

                                <button class="popup__item popup__item--button">
                                    <?= icon('times') ?>
                                    <?= _('Remove the links from the news') ?>
                                </button>
                            </form>

                            <div class="popup__separator"></div>

                            <form method="post" action="<?= url('unfollow collection', ['id' => $collection->id]) ?>" role="menuitem">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />

                                <button class="popup__item popup__item--button">
                                    <?= icon('feed-stop') ?>
                                    <?= _('Unfollow') ?>
                                </button>
                            </form>
                        </nav>
                    </details>
                <?php else: ?>
                    <form method="post" action="<?= url('follow collection', ['id' => $collection->id]) ?>">
                        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                        <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />
                        <button class="button--small">
                            <?= icon('feed') ?>
                            <?= _('Follow') ?>
                        </button>
                    </form>
                <?php endif; ?>
            <?php else: ?>
                <button
                    class="button--small"
                    data-controller="modal-opener"
                    data-action="modal-opener#fetch"
                    data-modal-opener-href-value="<?= url('login', ['redirect_to' => url('collection', $current_url_params)]) ?>"
                    aria-haspopup="dialog"
                    aria-controls="modal"
                >
                    <?= icon('feed') ?>
                    <?= _('Follow') ?>
                </button>
            <?php endif; ?>
        </div>
    </div>

    <?php if ($collection->description): ?>
        <div class="collection__description">
            <?= $collection->descriptionAsHtml() ?>
        </div>
    <?php endif; ?>

    <?php if ($pagination->numberElements() > 0): ?>
        <div class="collection__number-links">
            <?= _nf('%s link', '%s links', $pagination->numberElements(), format_number($pagination->numberElements())) ?>
        </div>
    <?php endif; ?>

    <?php if ($links): ?>
        <div class="cards cards--centered">
            <?php foreach ($links as $link): ?>
                <?= $this->include('links/_link.phtml', [
                    'link' => $link,
                    'from' => \Minz\Url::for('collection', $current_url_params),
                    'display_notes' => $collection->type !== 'feed',
                    'display_external_comments' => $collection->type === 'feed',
                    'display_hidden' => $collection->is_public && $link->is_hidden,
                    'display_read_later' => $current_user ? 'auto' : false,
                    'display_mark_as_read' => $current_user ? 'auto' : false,
                ]); ?>
            <?php endforeach; ?>
        </div>

        <?= $this->include('_pagination.phtml', ['pagination' => $pagination, 'url' => url('collection', ['id' => $collection->id])]) ?>
    <?php else: ?>
        <p class="paragraph--placeholder">
            <?= _('This collection is empty.') ?>
        </p>
    <?php endif; ?>
</div>
